[list_sort]
	List_Sort Version 1.0b1 -- Created by Johan Sšlve <mailto:johan@solve.se>
	This macro sorts a List using Server Side Java Script.
	Sorts with extended characters have not been fully tested.
	WARNING! This macro may crash your server due to SSJS reliability problems in Lasso 3.
			
	Usage examples: 
		[list_sort: 'listname']

#MACRO_BEGIN
[Lasso_Comment]

[Macro_Parent: #MACRO_INSERT_PARAMS#]
[var_set: 'macro_listsort_listname'=(macro_param: '1', EncodeNone)]
[/Macro_Parent]

<server>
	var mySession = new LassoSession();
	var lassolistname = mySession.getVariable("macro_listsort_listname");
	var lassolistCount = mySession.getVariableCount ( lassolistname );
	lassolist = new Array(lassolistCount);
	
	// sort comparison function
	function compareStrings(a, b) {
		if ( a < b ) return -1;
		if ( a > b ) return 1;
		return 0;
	}

	// put the Lasso list in a javascript array:
		for ( var count = 0; count < lassolistCount; ++count )
	{
		lassolist[count] = mySession.getVariable (lassolistname, count);
	}
	lassolist.sort(compareStrings);     // lexicographical sort

</server>
	// Empty the Lasso list with a nested loop to overcome the 1000 loops limitation
	[while: (List_ItemCount: (var: 'macro_listsort_listname', EncodeNone))>'1']
		[Loop: (List_ItemCount: (var: 'macro_listsort_listname', EncodeNone))]
			[List_RemoveItem: (var: 'macro_listsort_listname', EncodeNone), 
				ListIndex='1']
		[/Loop]
	[/while]
<server>
	// put the sorted array back into the Lasso list:
	for ( var count = 0; count < lassolistCount; ++count )
	{
		 mySession.setVariable (lassolistname, lassolist[count], count);
	}
</server>

[/Lasso_Comment]
#MACRO_END
